package com.jochen.core.ui.realize

import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.math.MathUtils
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import com.jochen.core.assets.AtlasAsset
import com.jochen.core.assets.FontAsset
import com.jochen.core.world.GdxGame

object UiStyle {
    
    const val DEFAULT_NAME = "UISTYLE"
    private val randomId = listOf<Int>(132,133,134,135,136,137,144,145,146,147)
    
    @JvmStatic
    fun getImageButtonStyle(atlas: TextureAtlas, overName: String): ImageButton.ImageButtonStyle {
        return getImageButtonStyle(atlas, overName, overName, overName)
    }
    
    @JvmStatic
    fun getImageButtonStyle(
        atlas: TextureAtlas,
        overName: String,
        upName: String,
        downName: String
    ): ImageButton.ImageButtonStyle {
        val ibs = ImageButton.ImageButtonStyle()
        if (overName != DEFAULT_NAME) {
            ibs.over = TextureRegionDrawable(atlas.findRegion(overName))
        }
        if (upName != DEFAULT_NAME) {
            ibs.up = TextureRegionDrawable(atlas.findRegion(upName))
        }
        if (downName != DEFAULT_NAME) {
            ibs.down = TextureRegionDrawable(atlas.findRegion(downName))
        }
        return ibs
    }
    
    fun getDrawable(atlas: TextureAtlas, name: String): TextureRegionDrawable {
        return TextureRegionDrawable(atlas.findRegion(name))
    }
    
    @JvmStatic
    fun getButtonStyle(game: GdxGame): TextButton.TextButtonStyle {
        val bs = TextButton.TextButtonStyle()
        bs.up = randDrawable(game)
        bs.down = randDrawable(game)
        bs.over = randDrawable(game)
        bs.font = game.assetService.get(FontAsset.MAIN_FONT) // 修正拼写 assetSerivce → assetService
        return bs
    }
    
    
    @JvmStatic
    fun randDrawable(game: GdxGame): TextureRegionDrawable {
        val index = MathUtils.random(0,randomId.size - 1)
        return TextureRegionDrawable(game.assetService.get(AtlasAsset.MENU).findRegion("${randomId.get(index)}"))
    }
}